package com.example.RemindMe;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.jBCrypt.BCrypt;


public class PWResetActivity extends Activity{


    //Felder
    private EditText reg_password1;
    private EditText reg_password2_1;
    private EditText reg_code1;
    private Button btnRegister1;

    //TextWatcher
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
        {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            checkFieldsForEmptyValues();
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pw_resetactivity);


        reg_password1 = (EditText) findViewById(R.id.reg_password1);
        reg_password2_1 = (EditText) findViewById(R.id.reg_password2_1);
        reg_code1 = (EditText) findViewById(R.id.reg_code1);
        btnRegister1 = (Button) findViewById(R.id.btnRegister1);


        //set listeners
        reg_password1.addTextChangedListener(textWatcher);
        reg_password2_1.addTextChangedListener(textWatcher);
        reg_code1.addTextChangedListener(textWatcher);

        // run once to disable if empty
        checkFieldsForEmptyValues();

    }


    private  void checkFieldsForEmptyValues(){
        String s1 = reg_password1.getText().toString();
        String s2 = reg_password2_1.getText().toString();
        String s3 = reg_code1.getText().toString();

        if (s1.length() > 0 && s2.length() > 0 && s3.length()>0 ) {
            btnRegister1.setEnabled(true);
        } else {
            btnRegister1.setEnabled(false);
        }
    }

    public void  resetPassword(View view){
        String s1 = reg_password1.getText().toString();
        String s2 = reg_password2_1.getText().toString();
        String s3 = reg_code1.getText().toString();

        SharedPreferences sharedPreferences = getSharedPreferences("codeEncrypted", MODE_PRIVATE);


        if(! s1.equals(s2))
        {
            Toast.makeText(this, "These passwords don't match. Try again?", Toast.LENGTH_LONG).show();
        }

        else if(BCrypt.checkpw(s3, sharedPreferences.getString("code", "DEFAULT")))
        {

            //Neues Passwort im SharedPref speichern
            sharedPreferences = getSharedPreferences("passwordEncrypted", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("pw", BCrypt.hashpw(s1,BCrypt.gensalt()));
            editor.commit();


            startActivity (new Intent(getApplicationContext(), DialogActivity.class));
            Toast.makeText(this,"Password got reset!",Toast.LENGTH_SHORT).show();
            finish();

        }

        else
        {
            Toast.makeText(this, "Code don't match. Try again?", Toast.LENGTH_LONG).show();

        }
    }


}
